<?php

namespace variant\library\vari;

use Seasx\SeasLogger\Logger;

class Log
{
    public static $logger;

    public static $instance;

    public function __construct()
    {
    }

    /**
     * 实例seasLog
     *
     * @return Logger
     */
    public static function instance()
    {
        if (!self::$logger instanceof Logger) {
            Logger::setLogger('http');
            self::$logger = new Logger();
            self::$logger->setBasePath(BT_PATH . 'log');
        }
        return self::$logger;
    }

    /**
     * 注册异常处理日志
     */
    public static function register()
    {
        self::instance();
        error_reporting(E_ALL);
        set_error_handler([__CLASS__, 'appError']);
        set_exception_handler([__CLASS__, 'appException']);
    }

    /**
     * 保存错误日志
     *
     * @param int $errNo
     * @param string $errMsg
     * @param string $errFile
     * @param int $errLine
     */
    public static function appError(int $errNo = 0, string $errMsg = '', string $errFile = '', int $errLine = 0)
    {
        $log = [
            $errNo,
            '|',
            $errMsg,
            '|',
            $errFile,
            '|',
            $errLine
        ];
        self::$logger->warning(implode(' ', $log) . PHP_EOL);
    }

    /**
     * 保存致命错误
     *
     * @param $exception
     */
    public static function appException($exception)
    {
        $log = [
            $exception->getCode(),
            '|',
            $exception->getMessage(),
            '|',
            $exception->getFile(),
            '|',
            $exception->getLine()
        ];
        self::$logger->error(implode(' ', $log) . PHP_EOL);
    }

    private function __clone()
    {
    }

}